from gibe.plugin import Plugin

from gibethemetechgeneral2008a.widgets import *
from gibethemetechgeneral2008a.controllers import *

from gibe.controllers import date_expand_day, date_expand_month, date_expand_year
from gibe.tags.plugin import tag_condition, tag_expand

import cherrypy

class TechGeneralPlugin(Plugin):
    def enabled(self):
        return bool(cherrypy.config.get('gibe.theme', None) == "techgeneral2008a")

    def initialise(self):
        import gibethemetechgeneral2008a.static.images
        images_dir = gibethemetechgeneral2008a.static.images.__path__[0]
        cherrypy.config.update({
            '/favicon.ico': {
                'static_filter.on': True,
                'static_filter.file': 'favicon.ico',
                'static_filter.root': images_dir,
            },
        })
        return True

    def template_search_path(self, blog, search_path):
        import gibethemetechgeneral2008a.templates
        search_path.insert(0, gibethemetechgeneral2008a.templates.__path__[0])

    def template_data(self, blog, template_data):
        td = {
            'gibethemetechgeneral2008a': GibeThemeTechGeneral2008a,
            'theme': GibeThemeTechGeneral2008a,
        }
        template_data.update(td)

    def template_replacements(self, blog, template_replacements):
        template_replacements['genshi:gibe.templates.frontpage'] = 'genshi:gibethemetechgeneral2008a.templates.frontpage'
        template_replacements['genshi:gibe.templates.post'] = 'genshi:gibethemetechgeneral2008a.templates.post'
        template_replacements['genshi:gibe.templates.comment_preview'] = 'genshi:gibethemetechgeneral2008a.templates.comment_preview'
        template_replacements['genshi:gibe.templates.archives'] = 'genshi:gibethemetechgeneral2008a.templates.archives'
        template_replacements['genshi:gibepages.templates.page'] = 'genshi:gibethemetechgeneral2008a.templates.page'
        template_replacements['genshi:gibe.templates.not_found'] = 'genshi:gibethemetechgeneral2008a.templates.not_found'
        pass

    def map_routes(self, blog, maps, controllers):
        controllers['gibethemetechgeneral2008a.TechGeneralController'] = TechGeneralController()

        #maps.append(('gibesitemap.sitemap_index', 'sitemap.xml', dict(controller='gibesitemap.SitemapController', action='index')))
        #maps.append(('gibesitemap.sitemap', 'sitemap_:(sitemap).xml', dict(controller='gibesitemap.SitemapController', action='sitemap')))

        for m in maps:
            if m[0] == "frontpage":
                frontpage_route_tuple = m
                break
        else:
            # Can't find "frontpage" route - guess we can't augment it then...
            return

        frontpage_route = list(frontpage_route_tuple)
        frontpage_route[2] = dict(controller='gibethemetechgeneral2008a.TechGeneralController', action="frontpage")
        i = maps.index(frontpage_route_tuple)
        maps[i] = tuple(frontpage_route)

        to_remove = []
        for m in maps:
            if m[0].startswith("archives"):
                to_remove.append(maps.index(m))
            if m[0].startswith("tags"):
                to_remove.append(maps.index(m))

        for i in sorted(to_remove, reverse = True):
            maps.pop(i)
    
        maps.append(('archives_day', 'archives/:year/:month/:day/', dict(controller='gibethemetechgeneral2008a.TechGeneralController', action='archives', _filter=date_expand_day)))
        maps.append(('archives_month', 'archives/:year/:month/', dict(controller='gibethemetechgeneral2008a.TechGeneralController', action='archives', _filter=date_expand_month)))
        maps.append(('archives_year', 'archives/:year/', dict(controller='gibethemetechgeneral2008a.TechGeneralController', action='archives', _filter=date_expand_year)))
        maps.append(('archives', 'archives', dict(controller='gibethemetechgeneral2008a.TechGeneralController', action='archives')))

        maps.append(('taglist', 'tags', dict(controller='gibethemetechgeneral2008a.TechGeneralController', action='taglist')))
        maps.append(('tags', 'tags/:(tagname)', dict(controller='gibethemetechgeneral2008a.TechGeneralController', action='tags', _filter=tag_expand, conditions=dict(function=tag_condition))))

        maps.append(('tags_rss', 'tags/:(tagname).rss', dict(controller='tagFeedController', action='rss2_0')))



